import { KeyCode } from "@/enums/App";
import PinyinT9 from "./PinyinT9_2";
import Hans from "./Hans";

const KeyMap = {
    [KeyCode.DOM_VK_2.value]: 2,
    [KeyCode.DOM_VK_3.value]: 3,
    [KeyCode.DOM_VK_4.value]: 4,
    [KeyCode.DOM_VK_5.value]: 5,
    [KeyCode.DOM_VK_6.value]: 6,
    [KeyCode.DOM_VK_7.value]: 7,
    [KeyCode.DOM_VK_8.value]: 8,
    [KeyCode.DOM_VK_9.value]: 9,
}

class HuangjiIMEConnect {

    name = "Huangji";

    initEmptyWord = true;
    cursorPosition = 0;
    keys: number[] = [];
    pinyins: string[] = [];

    candidateWord = "";

    constructor() {
        // let map = {};
        // Pinyin.forEach(py => {
        //     let n = "";
        //     py.split("").forEach(a => {
        //         for (const key in PinyinKeys) {
        //             if (PinyinKeys[key].includes(a)) {
        //                 n += key;
        //                 break;
        //             }
        //         }
        //     })
        //     if (!map[n]) {
        //         map[n] = [py];
        //     } else {
        //         map[n].push(py);
        //     }
        // })
        // console.log(map);
        // let items: any[] = [];
        // Object.keys(PinyinT9).forEach(py => {
        //     let arr = PinyinT9[py];
        //     arr.forEach(a => {
        //         items.push({ t9: py, py: a });
        //     })
        // })
        // console.log(items);
    }

    setLanguage(name) { }

    setLetter(prefix: number, keyCode: number) {
        if (__DEV__) {
            console.log("setLetter", prefix, keyCode);
        }
        if (prefix) {
            if (prefix == 224) {
                if (!this.pinyins.length) {
                    return;
                }
                switch (keyCode) {
                    case KeyCode.DOM_VK_RIGHT.value:
                        if (this.cursorPosition + 1 > this.pinyins.length - 1) {
                            this.cursorPosition = 0;
                        } else {
                            this.cursorPosition++;
                        }
                        break;
                    case KeyCode.DOM_VK_LEFT.value:
                        if (this.cursorPosition - 1 < 0) {
                            this.cursorPosition = this.pinyins.length - 1;
                        } else {
                            this.cursorPosition--;
                        }
                        break;
                    default:
                        return;
                }
            } else {
                return;
            }
        } else {
            this.cursorPosition = 0;
        }
        switch (keyCode) {
            case KeyCode.DOM_VK_RETURN.value:
                this.candidateWord = "";
                this.pinyins = [];
                this.keys = [];
                return;
            case KeyCode.DOM_VK_BACK_SPACE.value:
                this.keys.splice(-1, 1);
                break;
            default:
                if (KeyMap[keyCode]) {
                    this.keys.push(KeyMap[keyCode]);
                }
                break;
        }
        if (!this.keys.length) {
            this.candidateWord = "";
            return;
        }
        const pyKey = this.keys.join("");
        // const pyItems = PinyinT9[pyKey];
        const pyItems = PinyinT9.filter(o => o.t9 == pyKey).map(o => o.py);
        if (!pyItems.length) {
            pyItems.push(...PinyinT9.filter(o => {
                return o.t9.length - pyKey.length > 0 && o.t9.startsWith(pyKey);
            }).slice(0, 3).map(o => o.py));
        }
        if (pyItems.length) {
            this.pinyins = pyItems;
            const pySelected = this.pinyins[this.cursorPosition];
            let word = "";
            if (pySelected) {
                const hans = Hans[pySelected];
                if (hans) {
                    word = hans.split("").join("|") + "|";
                }
            }
            this.candidateWord = `${this.pinyins.map((o, index) => {
                if (o == pySelected) {
                    return `>${o}`;
                }
                return o;
            }).join("|")}||${word}`;
        } else {
            this.pinyins = [];
            // this.candidateWord = `>${pyKey}||`;
            this.candidateWord = "";
        }
        if (__DEV__) {
            console.log(this.candidateWord);
        }
    }

    setLetterMultiTap(code, tapCount, lastCharCode) { }
}

window["HuangjiIMEConnect"] = HuangjiIMEConnect;